package com.ashin.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SimpleGlobalException implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        HandlerMethod handler1 = (HandlerMethod) handler;
        ResponseBody methodAnnotation = handler1.getMethodAnnotation(ResponseBody.class);
        if (methodAnnotation == null) {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("testException");

            return modelAndView;
        } else {
            ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
            modelAndView.addObject("testexccccc");

            return modelAndView;
        }
    }
}
